home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 01 / wintro4 / fileopen.c < prev    next >
C/C++ Source or Header  |  1990-12-31  |  5KB  |  173 lines

  1. /*===========================================================================*/
  2. /*                                                                           */
  3. /* File    : FILEOPEN.C                                                      */
  4. /*                                                                           */
  5. /* Purpose : Standard file-open dialog box processing. Taken from the        */
  6. /*           sample code in the Windows 3.0 SDK.                             */
  7. /*                                                                           */
  8. /* History :                                                                 */
  9. /*                                                                           */
  10. /*===========================================================================*/
  11.  
  12. #include <string.h>
  13. #include <windows.h>
  14. #include "stock.h"
  15.  
  16. char FileName[128];
  17. char PathName[128];
  18. char OpenName[128];
  19. char DefPath[128];
  20. char DefSpec[13] = "*.sto";
  21. char DefExt[] = ".sto";
  22. char str[255];
  23.  
  24.  
  25. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  26.   HWND hDlg;
  27.   unsigned message;
  28.   WORD wParam;
  29.   LONG lParam;
  30. {
  31.   WORD index;
  32.   PSTR pTptr;
  33.   HANDLE hFile;
  34.  
  35.   switch (message)
  36.   {
  37.     case WM_COMMAND:
  38.       switch (wParam)
  39.       {
  40.         case IDC_LISTBOX:
  41.            switch (HIWORD(lParam))
  42.            {
  43.              case LBN_SELCHANGE:
  44.                if (!DlgDirSelect(hDlg, str, IDC_LISTBOX))
  45.                {
  46.                  SetDlgItemText(hDlg, IDC_EDIT, str);
  47.                  SendDlgItemMessage(hDlg,
  48.                                     IDC_EDIT,
  49.                                     EM_SETSEL,
  50.                                     NULL,
  51.                                     MAKELONG(0, 0x7fff));
  52.                }
  53.                else
  54.                {
  55.                  strcat(str, DefSpec);
  56.                  DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  57.                }
  58.                break;
  59.  
  60.              case LBN_DBLCLK:
  61.                goto openfile;
  62.            }
  63.            return TRUE;
  64.  
  65.          case IDOK:
  66. openfile:
  67.            GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  68.            if (strchr(OpenName, '*') || strchr(OpenName, '?'))
  69.            {
  70.              SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec, (LPSTR) OpenName);
  71.              if (str[0])
  72.                strcpy(DefPath, str);
  73.              ChangeDefExt(DefExt, DefSpec);
  74.              UpdateListBox(hDlg);
  75.              return TRUE;
  76.            }
  77.            if (!OpenName[0])
  78.            {
  79.              MessageBox(hDlg, "No filename specified.", NULL, MB_OK | MB_ICONHAND);
  80.              return TRUE;
  81.            }
  82.  
  83.            AddExt(OpenName, DefExt);
  84.  
  85.            /* The routine to open the file would go here, and the */
  86.            /* handle would be returned instead of NULL.           */
  87.            StockFileRead((LPSTR) OpenName);
  88.  
  89.            EndDialog(hDlg, hFile);
  90.            return (TRUE);
  91.  
  92.           case IDCANCEL:
  93.             EndDialog(hDlg, NULL);
  94.             return (TRUE);
  95.           }
  96.           break;
  97.  
  98.         case WM_INITDIALOG:                        /* message: initialize    */
  99.             UpdateListBox(hDlg);
  100.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  101.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  102.                 IDC_EDIT,                          /* where to send message  */
  103.                 EM_SETSEL,                         /* select characters      */
  104.                 NULL,                              /* additional information */
  105.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  106.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  107.             return (FALSE); /* Indicates the focus is set to a control */
  108.     }
  109.     return FALSE;
  110. }
  111.  
  112.  
  113. void UpdateListBox(hDlg)
  114.   HWND hDlg;
  115. {
  116.   strcpy(str, DefPath);
  117.   strcat(str, DefSpec);
  118.   DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  119.   SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  120. }
  121.  
  122.  
  123. void ChangeDefExt(Ext, Name)
  124.   PSTR Ext, Name;
  125. {
  126.   PSTR pTptr;
  127.  
  128.   pTptr = Name;
  129.   while (*pTptr && *pTptr != '.')
  130.     pTptr++;
  131.   if (*pTptr)
  132.     if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  133.       strcpy(Ext, pTptr);
  134. }
  135.  
  136.  
  137. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  138.   HWND hDlg;
  139.   LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  140. {
  141.   LPSTR lpTmp;
  142.   char  cTmp;
  143.  
  144.   lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  145.   while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  146.     lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  147.   if (*lpTmp != ':' && *lpTmp != '\\')
  148.   {
  149.     lstrcpy(lpDestFileName, lpSrcFileName);
  150.     lpDestPath[0] = 0;
  151.     return;
  152.   }
  153.   lstrcpy(lpDestFileName, lpTmp + 1);
  154.   cTmp = *(lpTmp + 1);
  155.   lstrcpy(lpDestPath, lpSrcFileName);
  156.   *(lpTmp + 1) = cTmp;
  157.   lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  158. }
  159.  
  160.  
  161. void AddExt(Name, Ext)
  162.   PSTR Name, Ext;
  163. {
  164.   PSTR pTptr;
  165.  
  166.   pTptr = Name;
  167.   while (*pTptr && *pTptr != '.')
  168.     pTptr++;
  169.   if (*pTptr != '.')
  170.     strcat(Name, Ext);
  171. }
  172.  
  173.